package com.mostromas.ess.project.controller;


import com.mostromas.ess.entities.ProjectDetailed;
import com.mostromas.ess.entities.UserProject;
import com.mostromas.ess.feign.IFeignFileController;
import com.mostromas.ess.feign.IFeignProjectController;
import com.mostromas.ess.project.req.ProjectDetailedREQ;
import com.mostromas.ess.project.service.IProjectDetailedService;
import com.mostromas.ess.project.service.IUserProjectsService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Api(value = "软件信息管理接口", description = "提供软件信息的添加和删除")
@RestController
@RequestMapping("/detailed")
public class ProjectDetailedController {

    @Autowired
    private IFeignProjectController feignProjectController;

    @Autowired
    private IProjectDetailedService projectDetailedService;

    @Autowired
    private IUserProjectsService userProjectsService;

    @PreAuthorize(("hasAuthority('detailed:search')"))
    @ApiOperation("根据软件名称和简称查询软件列表")
    @PostMapping("/search")
    public Result search(@RequestBody ProjectDetailedREQ req) {
        return projectDetailedService.queryList(req);
    }

    @PreAuthorize(("hasAuthority('detailed:search')"))
    @ApiOperation("查询软件详情接口")
    @ApiImplicitParam(name = "id",value = "软件ID",required = true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return Result.ok(projectDetailedService.getById(id));
    }

    @PreAuthorize(("hasAuthority('detailed:add')"))
    @ApiOperation("新增软件信息接口")
    @PostMapping
    public Result save(@RequestBody ProjectDetailed info) {
        projectDetailedService.saveOrUpdate(info);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('detailed:update')"))
    @ApiOperation("新增/修改软件使用期限接口")
    @PostMapping("/updateEffectiveTime")
    public Result updateEffectiveTime(@RequestBody UserProject info) {
        return userProjectsService.updateEffectiveTime(info);
    }

    @PreAuthorize(("hasAuthority('detailed:delete')"))
    @ApiOperation("删除软件信息接口")
    @ApiImplicitParam(name = "id", value = "软件ID", required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        feignProjectController.delete(projectDetailedService.getById(id).getUrl());
        projectDetailedService.removeById(id);
        return Result.ok();
    }
}